﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using Yoozoo.Framework.Managers;

namespace Yoozoo.Gta.Common
{
    public class ResidentHandler : MonoBehaviour
    {
        
        public GameObject RootCanvas;
        public Canvas RootCanvasComponent;
        public Camera UICamera;

        [Range(0.1f,120f)]
        public float clearTime = 20f;
        private float currentTime = 0f;
        
        [Range(0.01f,10f)]
        public float maxHoldTime = 5f;

        private Dictionary<string, LuaFunction> m_luaFunctionMap;
        private Dictionary<string, float> m_funcHoldTimeMap;
        //private Dictionary<string, LuaTable> m_luaTableMap;
        
        
        public static ResidentHandler Inst;

        public void Awake()
        {
            Inst = this;
            Init();
        }

        private void Init()
        {
            RootCanvas = GameObject.Find("/UIRoot/RootCanvas");
            UICamera = GameObject.Find("UIRoot/UICamera").GetComponent<Camera>();
            RootCanvasComponent = RootCanvas.GetComponent<Canvas>();

            m_luaFunctionMap = new Dictionary<string, LuaFunction>(100);
            m_funcHoldTimeMap = new Dictionary<string, float>(100);
            //m_luaTableMap = new Dictionary<string, LuaTable>(10);

            m_removeKeys = new List<string>(100);
        }

        private List<string> m_removeKeys;
        private void Update()
        {
            currentTime += Time.deltaTime;
            if (currentTime > clearTime)
            {
                m_removeKeys.Clear();
                foreach (var f in m_funcHoldTimeMap)
                {
                    if ((Time.time - f.Value) > maxHoldTime)
                    {
                        m_removeKeys.Add(f.Key);
                    }
                }

                foreach (var removeKey in m_removeKeys)
                {
                    RemoveFunction(removeKey);
                }
                
                currentTime = 0;
            }
            
        }
        
        public LuaFunction GetFunction(string key)
        {
            LuaFunction f;
            if (m_luaFunctionMap.TryGetValue(key,out f))
            {
                m_funcHoldTimeMap[key] = Time.time;
                return f;
            }
            f = LuaManager.mainState.GetFunction(key);
            m_luaFunctionMap[key] = f;
            m_funcHoldTimeMap[key] = Time.time;
            return f;
        }

        public void RemoveFunction(string key)
        {
            if (m_luaFunctionMap.ContainsKey(key))
            {
                m_luaFunctionMap.Remove(key);
            }
            if (m_funcHoldTimeMap.ContainsKey(key))
            {
                m_funcHoldTimeMap.Remove(key);
            }
        }
        
        // public LuaTable GetLuaTable(string key)
        // {
        //     LuaTable luaTable;
        //     if (m_luaTableMap.TryGetValue(key,out luaTable))
        //     {
        //         return luaTable;
        //     }
        //
        //     luaTable = LuaManager.mainState.GetLuaTable(key);
        //     m_luaTableMap[key] = luaTable;
        //     return luaTable;
        // }


        public void Clear()
        {
            m_luaFunctionMap.Clear();
            m_funcHoldTimeMap.Clear();
        }
        
    }
}
